home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
C/C++ Users Group Library 1996 July
/
C-C++ Users Group Library July 1996.iso
/
vol_200
/
297_01
/
pralloc.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-12-30
|
18KB
|
603 lines
/* pralloc.c */
/* allocation */
/* 21/12/91
dump_stack renamed dump_ancestors
*/
#include <stdio.h>
#include "prtypes.h"
#define NDEBUG 1 /* turn off checking */
#define PAIRS_TOGETHER 1
/* Only pairs are allocated on heap.
* This is useful in the case of 8086 architercture.
* as pairs take up a lot of room.
*/
/* define STATISTICS used to see how much each structure takes */
#define DEFAULTSIZE 32000 /* default memory zone size */
#define BUFFSIZE 1000 /* io buffer sizes */
/* error messages */
#define OVERFLOW "overflow"
#define SUBSTSPACE "substitution stack"
#define DYNSPACE "control stack"
#define TRAILSPACE "trail"
#define HEAPSPACE "heap"
#define STRINGSPACE "string zone"
#define TEMPSPACE "temp"
#define SEESTACK "Stack dump?(y/n)"
#define WILDPOINTER "stray pointer!"
/* byte alignment */
#define ALIGN(X) X>>=2,X<<=2,X+=4;
/* #define CAN_ALLOC(HOWMUCH, MAXPTR, CURRPTR) (((MAXPTR)-(CURRPTR)) > HOWMUCH)*/
#define CAN_ALLOC(HOWMUCH, MAXPTR, CURRPTR) ((CURRPTR + HOWMUCH) < MAXPTR)
/* This macro is used in the following circumstance:
* MAXPTR is the top of a zone,
* CURRPTR is the current pointer in the zone, it is less than MAXPTR.
* HOWMUCH is an integer that says how much you want to increase CURRPTR
* but you must stay less than MAXPTR.
*/
#define ADDRESS_DIFF(PTR1, PTR2) (char *)(PTR1) - (char *)(PTR2)
typedef char *void_ptr_t;
void alloc_err(), fatal_alloc_err(), clean_pred();
#ifdef STATISTICS
/* These numbers let you monitor how much the different
* structures consume.
* It may suprise you to know that integers and variables
* consume nothing because they are stored directly in a node.
*/
zone_size_t Atom_consumption;
zone_size_t Pair_consumption;
zone_size_t Node_consumption;
zone_size_t Clause_consumption;
zone_size_t String_consumption;
zone_size_t Predrec_consumption;
#ifdef REAL
zone_size_t Real_consumption;
#endif
#ifdef CHARACTER
zone_size_t Char_consumption;
#endif
#endif
char *Read_buffer; /* read tokens into this */
char *Print_buffer; /* used by pr_string */
clause_ptr_t Bltn_pseudo_clause; /* see prlush.c */
node_ptr_t Printing_var_nodeptr; /* see prprint.c */
static char *Heap_mem; /* bottom of heap */
static char *Heap_ptr; /* allocate from this and move this up */
static char *HighHeap_ptr; /* top of heap */
static char *Str_mem; /* this is used to allocate permanent strings
and perhaps other objects if you want to keep the pairs
together
*/
static char *Str_ptr; /* allocate from this and move this up */
static char *HighStr_ptr; /* top of zone */
dyn_ptr_t Dyn_mem; /* bottom of control stack
(and zone for those temporary objects that disappear on backtrack,
although the substitution zone could do as well) */
dyn_ptr_t Dyn_ptr; /* allocate from this and move this up */
dyn_ptr_t HighDyn_ptr; /* top of control stack */
node_ptr_t **Trail_mem; /* the trail (used to reset variable bindings) */
node_ptr_t **Trail_ptr; /* like the others */
node_ptr_t **HighTrail_ptr;/* top of zone */
subst_ptr_t Subst_mem; /* bottom of (global) variable bindings stack */
subst_ptr_t Subst_ptr; /* allocate from this and move this up */
subst_ptr_t HighSubst_ptr;/* top of zone */
temp_ptr_t Temp_mem; /* For things you might want to
create with temp_assert...
clean with clean_temp */
temp_ptr_t Temp_ptr; /* allocate from this and move this up */
temp_ptr_t HighTemp_ptr;/* top of zone */
int Max_Readbuffer, Max_Printbuffer;
atom_ptr_t Nil;
node_ptr_t NilNodeptr;
/*******************************************************************************
ini_alloc()
Reserve zones and io buffers.
******************************************************************************/
void ini_alloc()
{
zone_size_t heap_size, str_size, dyn_size, trail_size, subst_size, temp_size;
extern void_ptr_t os_alloc(); /* see the machine dependent file */
extern int read_config(); /* see the machine dependent file */
if(!read_config(&heap_size, &str_size, &dyn_size, &trail_size, &subst_size, &temp_size,
&Max_Readbuffer, &Max_Printbuffer))
{
heap_size = DEFAULTSIZE;
str_size = DEFAULTSIZE;
dyn_size = DEFAULTSIZE;
trail_size = DEFAULTSIZE;
subst_size = DEFAULTSIZE;
temp_size = DEFAULTSIZE;
Max_Readbuffer = BUFFSIZE;
Max_Printbuffer = BUFFSIZE;
}
Heap_mem = os_alloc(heap_size);
Heap_ptr = Heap_mem;
HighHeap_ptr = Heap_mem + heap_size;
Str_mem = os_alloc(str_size);
Str_ptr = Str_mem;
HighStr_ptr = Str_mem + str_size;
Dyn_mem = (dyn_ptr_t)os_alloc(dyn_size);
Dyn_ptr = Dyn_mem;
HighDyn_ptr = (dyn_ptr_t)((char *)Dyn_mem + dyn_size);
Trail_mem = (node_ptr_t **)os_alloc(trail_size);
Trail_ptr = Trail_mem;
HighTrail_ptr = (node_ptr_t **)((char *)Trail_mem + trail_size);
Subst_mem = (subst_ptr_t)os_alloc(subst_size);
Subst_ptr = Subst_mem;
HighSubst_ptr = (subst_ptr_t)((char *)Subst_mem + subst_size);
while(Subst_ptr < HighSubst_ptr)
{
Subst_ptr->skel = (node_ptr_t)NULL;
Subst_ptr++;
}
Subst_ptr = Subst_mem;
Temp_mem = (temp_ptr_t)os_alloc(temp_size);
Temp_ptr = Temp_mem;
HighTemp_ptr = (temp_ptr_t)((char *)Temp_mem + temp_size);
Read_buffer = os_alloc((zone_size_t)Max_Readbuffer);
Print_buffer = os_alloc((zone_size_t)Max_Printbuffer);
}
/*******************************************************************************
my_Heap_alloc()
******************************************************************************/
static void_ptr_t my_Heap_alloc(how_much)
my_alloc_size_t how_much;
{
void_ptr_t ret;
ALIGN(how_much);
if(!CAN_ALLOC(how_much ,HighHeap_ptr, Heap_ptr))
{
fatal_alloc_err(HEAPSPACE);
}
else
ret = Heap_ptr;
Heap_ptr += how_much;
return(ret);
}
/*******************************************************************************
my_Str_alloc()
Allocate on permanent string space.
******************************************************************************/
static void_ptr_t my_Str_alloc(how_much)
my_alloc_size_t how_much;
{
void_ptr_t ret;
ALIGN(how_much);
if(!(CAN_ALLOC(how_much, HighStr_ptr, Str_ptr)))
{
fatal_alloc_err(STRINGSPACE);
}
else
ret = Str_ptr;
Str_ptr += how_much;
return(ret);
}
/********************************************************************************
my_Dyn_alloc()
Allocate on the control stack.
This is for objects that disappear on backtracking.
******************************************************************************/
dyn_ptr_t my_Dyn_alloc(how_much)
my_alloc_size_t how_much; /* in bytes */
{
dyn_ptr_t ret;
ALIGN(how_much);
if(!(CAN_ALLOC(how_much ,HighDyn_ptr, Dyn_ptr)))
{
alloc_err(DYNSPACE);
}
else
ret = Dyn_ptr;
Dyn_ptr += how_much;
return(ret);
}
/*******************************************************************************
my_Trail_alloc()
Allocate one trail element.
******************************************************************************/
node_ptr_t ** my_Trail_alloc()
{
node_ptr_t ** ret;
if( Trail_ptr >= HighTrail_ptr)
{
alloc_err(TRAILSPACE);
}
else
ret = Trail_ptr;
Trail_ptr ++;
return(ret);
}
/*******************************************************************************
my_Subst_alloc()
Allocate how_much bytes on the substitution stack.
This is more speed-efficient than allocating struct substs on an array of
structures, as there is no multiplication.
******************************************************************************/
subst_ptr_t my_Subst_alloc(how_much)
my_alloc_size_t how_much; /* in bytes */
{
subst_ptr_t ret;
#ifndef NDEBUG
if(how_much % sizeof(struct subst))INTERNAL_ERROR("alignment");
#endif
if(! CAN_ALLOC(how_much ,HighSubst_ptr, Subst_ptr))
{
alloc_err(SUBSTSPACE);
}
else
ret = Subst_ptr;
Subst_ptr += how_much;
return(ret);
}
/********************************************************************************
my_Temp_alloc()
Allocate in temporary memory.
This is for objects that disappear
when y